package com.sky.service.Workspace;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.UserMapper;
import com.sky.vo.BusinessVO;
import com.sky.vo.OverViewDishesVO;
import com.sky.vo.OverViewOrdersVO;
import com.sky.vo.OverViewSetmealsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 褚金龙
 * @Date 2024/10/18 12:27
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService{
   @Autowired
   private OrderMapper orderMapper;

   @Autowired
   private UserMapper userMapper;

   @Autowired
   private DishMapper dishMapper;

   @Autowired
   private SetmealDishMapper setmealDishMapper;



    /**
     * 获取工作台-今日数据
     * @return
     */
    @Override
    public BusinessVO getTodayData( LocalDateTime beginTime,LocalDateTime endTime) {
        //查询营业额，当日已完成订单的总金额
       // 获取今日开始时间和结束时间


        Map map=new HashMap();
        map.put("beginTime",beginTime);
        map.put("endTime",endTime);
        //查询订单总数
        Integer totalOrderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //1.当日完成订单的总金额
          Double turnover  =  orderMapper.sumByMap(map);
          turnover= turnover==null?0.0:turnover;


         //2.有效订单的个数
        Integer ValidOrderCount = orderMapper.countByMap(map);

        //3.平均客单价
        Double unitPrice=0.0;

         //4.订单完成率
        Double orderCompletionRate=0.0;

        if(totalOrderCount!=0&&ValidOrderCount!=0){
            //订单完成率
            orderCompletionRate = (ValidOrderCount.doubleValue() / totalOrderCount);

            //平均客单价
            unitPrice=turnover/ValidOrderCount;
        }

       // 5 新增用户数
        Integer newUserCount= userMapper.countByMap(map); //获取每一天新增的用户总数
        return BusinessVO.builder()
                .turnover(turnover)
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUserCount)
                .unitPrice(unitPrice)
                .validOrder(ValidOrderCount)
                .build()
                ;


    }

    /**
     * 订单管理接口数据
     *
     * @return
     */
    @Override
    public OverViewOrdersVO getOverviewOrders() {

        LocalDateTime endTime=LocalDateTime.now().with(LocalTime.MIN);
        Map map=new HashMap();
        map.put("beginTime",endTime);

        //1.待接单订单数量

        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);
        //2.待派送订单数量

        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //3.已完成订单数量

        map.put("status",Orders.COMPLETED);

        Integer completedOrders = orderMapper.countByMap(map);

        //4.已取消订单数量
        map.put("status",Orders.CANCELLED);
        Integer canceledOrders = orderMapper.countByMap(map);

        //5.获取全部订单数量

        map.put("status",null);
        Integer allOrders = orderMapper.countByMap(map);




        return OverViewOrdersVO.builder()
                .allOrders(allOrders)
                .canceledOrders(canceledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build()
                ;
    }


    /**
     * 菜品总览
     * @return
     */
    @Override
    public OverViewDishesVO getOverviewDishes() {
        LocalDateTime endTime=LocalDateTime.now().with(LocalTime.MIN);
        Map map=new HashMap();
        map.put("endTime",endTime);
        //1.已停售菜品总数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued= dishMapper.countByMap();

        //2.已启售菜品总数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold= dishMapper.countByMap();

        return OverViewDishesVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build()
                ;
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public OverViewSetmealsVO getOverviewSetmeals() {
        LocalDateTime endTime=LocalDateTime.now().with(LocalTime.MIN);
        Map map=new HashMap();
        map.put("endTime",endTime);
        //1.已停售菜品总数量
        map.put("status", StatusConstant.DISABLE_SETMEAL);
        Integer discontinued= setmealDishMapper.countByMap();

        //2.已启售菜品总数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold= setmealDishMapper.countByMap();

        return OverViewSetmealsVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build()
                ;


    }
}
